//
//  OrderServer.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/21.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "OrderServer.h"
#import "HELOrderModel.h"

@interface OrderServer ()

@property(nonatomic, strong)MBProgressHUD *hud;



@end

@implementation OrderServer


//创建订单
- (void)createOrder:(NSInteger)courseId withView:(UIView *)view {
    
    
    cwqHud(view);
    self.hud = hud;
 
    [[HELNetworkManager sharedMg] POST:helCreateOrder parameters:@{@"courseId":@(courseId)} success:^(id responseObject) {
        
    
        if (![responseObject[@"success"] isKindOfClass:[NSNull class]]  &&  [responseObject[@"success"] integerValue] == 1) {
            
            [self getOrderWith:responseObject[@"data"]];
        }else{
           
            
        }
        
        
    } failure:^(NSString *err) {
        [self.hud hide:YES];
    }];
    
}


//预览订单
- (void)getOrderWith:(NSString *)str{
    
    
  
    
    
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helPreviewOrder,str] parameters:nil success:^(id responseObject) {
     
        if (![responseObject[@"data"] isKindOfClass:[NSNull class]]  && responseObject[@"data"][@"order"]) {
            
             HELOrderModel *order = [HELOrderModel mj_objectWithKeyValues:responseObject[@"data"][@"order"]];
            
            [self confirmOrderWithOrderId:order.id];
        }else{
         
        }
        
        
    } failure:^(NSString *err) {
        [self.hud hide:YES];
    }];
    
}


//提交订单

- (void)confirmOrderWithOrderId:(NSString *)orderId{
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
  
   
//     params[@"couponId"] = model.id;
     params[@"amountPay"] = @(0);
    
    
    [[HELNetworkManager sharedMg] POST:[NSString stringWithFormat:@"%@/%@",helConfirmOrder,orderId] parameters:params success:^(id responseObject) {
        [self.hud hide:YES];
        if (![responseObject[@"success"] isKindOfClass:[NSNull class]] && [responseObject[@"success"] integerValue]) {
           
            [self getPayStringWith:orderId];
            
            
        }
        
    } failure:^(NSString *err) {
          [self.hud hide:YES];
    }];
    
}



//获取支付字符串
- (void)getPayStringWith:(NSString *)orderId{
    
  
  
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helGetOrderString,orderId] parameters:nil success:^(id responseObject) {
      
        
        [self.hud hide:YES];
        if ([responseObject[@"data"][@"status"] isEqualToString:@"pay"]) {
            
            
            
//            weakSelf.payInfo = responseObject[@"data"][@"payInfo"];
            
            
            //不需要支付
        }else if ([responseObject[@"data"][@"status"] isEqualToString:@"no_pay"]){
            
            cwqHudSuccess(@"参加课程成功~");
          
            NoficationCenter_post(RefreshCourseDetail);
            NoficationCenter_post(RefreshCourseContent);
            
            
        }else if ([responseObject[@"data"][@"status"] isEqualToString:@"no_pay"]){
            
            
            
        }
        
        
    } failure:^(NSString *err) {
       
        [self.hud hide:YES];
        
    }];
    
}


@end
